package com.zhuoyue.system.service.impl;

import com.zhuoyue.common.core.domain.entity.SysDictData;
import com.zhuoyue.common.core.domain.entity.TblProductBrand;
import com.zhuoyue.common.core.domain.entity.TblProductSeries;
import com.zhuoyue.common.utils.DateUtils;
import com.zhuoyue.system.domain.TblProductInfo;
import com.zhuoyue.system.mapper.ProductInfoMapper;
import com.zhuoyue.system.service.ISysDictDataService;
import com.zhuoyue.system.service.IProductInfoService;
import com.zhuoyue.system.service.ISysProductManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * imageService业务层处理
 *
 * @author hewenze
 * @date 2020-08-21
 */
@Service
public class ProductInfoServiceImpl implements IProductInfoService {
    @Autowired
    private ProductInfoMapper sysImageMapper;

    @Autowired
    private ISysProductManageService productManageService;

    @Autowired
    private ISysDictDataService dictDataService;

    /**
     * 查询image
     *
     * @param id imageID
     * @return image
     */
    @Override
    public TblProductInfo selectProductInfoById(String id) {
        return sysImageMapper.selectProductInfoById(id);
    }

    /**
     * 查询产品列表
     *
     * @param sysImage image
     * @return image
     */
    @Override
    public List<TblProductInfo> selectProductInfoList(TblProductInfo sysImage) {
        //查询所需的产品信息, 填充对应的品牌, 分类, 产品功能等数据
        List<TblProductInfo> productInfos = sysImageMapper.selectProductInfoList(sysImage);

        // 查询所有的品牌
        List<TblProductBrand> productBrands = productManageService.selectBrandList(new TblProductBrand());
        // 查询所有的系列
        List<TblProductSeries> seriesList = productManageService.selectSeriesList(new TblProductSeries());
        // 查询所有的字典值
        List<SysDictData> dictDataList = dictDataService.selectDictDataList(new SysDictData());

        for (TblProductInfo productInfo : productInfos) {
            // 获取该产品信息的品牌
            for (TblProductBrand productBrand : productBrands) {
                if (Objects.equals(productBrand.getId(), productInfo.getBrandId())) {
                    productInfo.setProductBrand(productBrand);
                }
            }

            // 获取该产品信息的分类列表
            List<TblProductSeries> seriesList1 = new ArrayList<>();
            // 该产品所属的系列
            String[] seriesIdStringList = StringUtils.delimitedListToStringArray(productInfo.getSeriesIdList(), ",");
            for (String seriesIdString : seriesIdStringList) {
                for (TblProductSeries productSeries : seriesList) {
                    if (productSeries.getId() == Long.parseLong(seriesIdString)) {
                        seriesList1.add(productSeries);
                    }
                }
            }
            productInfo.setProductSeriesList(seriesList1);

            // 获取该产品的字典值
            List<SysDictData> sysDictDataList = new ArrayList<>();
            String[] dictCodeStringList = StringUtils.delimitedListToStringArray(productInfo.getDictCodeList(), ",");
            for (String dictCodeString : dictCodeStringList) {
                for (SysDictData sysDictData : dictDataList) {
                    if (sysDictData.getDictCode() == Long.parseLong(dictCodeString)) {
                        sysDictDataList.add(sysDictData);
                    }
                }
            }
            productInfo.setDictDataList(sysDictDataList);
        }

        return productInfos;
    }

    @Override
    public List<TblProductInfo> selectProductInfoListForApp(TblProductInfo sysImage) {
        return sysImageMapper.selectProductInfoListForApp(sysImage);
    }

    /**
     * 新增image
     *
     * @param sysImage image
     * @return 结果
     */
    @Override
    public int insertSysImage(TblProductInfo sysImage) {
        sysImage.setCreateTime(DateUtils.getNowDate());
        return sysImageMapper.insertProductInfo(sysImage);
    }

    /**
     * 修改image
     *
     * @param sysImage image
     * @return 结果
     */
    @Override
    public int updateProductInfo(TblProductInfo sysImage) {
        return sysImageMapper.updateProductInfo(sysImage);
    }

    /**
     * 批量删除image
     *
     * @param productInfoId 需要删除的imageID
     * @return 结果
     */
    @Override
    public int deleteSysImageByIds(String productInfoId) {
        return sysImageMapper.deleteProductInfoByIds(productInfoId);
    }

    /**
     * 删除image信息
     *
     * @param id imageID
     * @return 结果
     */
    @Override
    public int deleteSysImageById(Long id) {
        return sysImageMapper.deleteProductInfoById(id);
    }

    @Override
    public int getCountOfZdrwInOneSeries(Long seriesId) {
        return sysImageMapper.getCountOfZdrwInOneSeries(seriesId);
    }

    @Override
    public int setZdrwDisableOfCategory(Long categoryId) {
        return sysImageMapper.setZdrwDisableOfCategory(categoryId);
    }

    @Override
    public LocalDateTime getLatestUpdateTimeOfApp() {
        return sysImageMapper.getLatestUpdateTimeOfApp();
    }
}
